package com.googlecode.iiddefecttracker.sample;

import static com.googlecode.iiddefecttracker.sample.DefectTestData.allDefectTestData;
import static com.googlecode.iiddefecttracker.sample.ProjectTestData.allProjectTestData;

import org.springframework.jdbc.core.JdbcTemplate;

import com.googlecode.iiddefecttracker.domain.Defect;
import com.googlecode.iiddefecttracker.domain.Project;

/**
 * 
 */
public class DataPopulatorImpl implements DataPopulator {
    
    public void insertDefectTestData(final JdbcTemplate template) {
        
        int identityStartsWith = 1;
        for (final Defect defect : allDefectTestData()) {
            
            String sql = "INSERT INTO defects (project_id, summary, stepstorecreate, result, expectedresult, workaround) VALUES ("
                    + defect.getProject().getId()
                    + ", '"
                    + defect.getSummary()
                    + "', '"
                    + defect.getStepsToRecreate()
                    + "', '"
                    + defect.getResult()
                    + "', '"
                    + defect.getExpectedResult()
                    + "', ";
            
            if (defect.getWorkaround() == null) {
                sql += "NULL";
            } else {
                sql += "'" + defect.getWorkaround() + "'";
            }
            sql += ");";
            
            template.execute(sql);
            
            defect.setId(Long.valueOf(identityStartsWith));
            identityStartsWith++;
        }
    }
    
    public void insertProjectTestData(final JdbcTemplate template) {
        int identityStartsWith = 1;
        for (final Project project : allProjectTestData()) {
            template
                    .execute("INSERT INTO projects (name, version, description) VALUES ('"
                            + project.getName()
                            + "', '"
                            + project.getVersion()
                            + "', '" + project.getDescription() + "');");
            
            project.setId(Long.valueOf(identityStartsWith));
            identityStartsWith++;
        }
    }
}
